﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.Utils;
using Microsoft.Practices.Unity;
using Baosight.Platform.Core.UI;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCM.UI.ChildWindows;



namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 领料申请从表  
    /// </summary>
    /// Title:ZMCM84
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张坤
    /// Version: 1.0
    /// History:
    ///       2011-06-21  张坤   [创建]
    ///
    public partial class ZMCM84 : EFViewCmes
    {
        public ZMCM84()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCM84_LayoutUpdated);
        }

        /// <summary>
        /// 主表已审批，明细表按钮失效方法
        /// 此处根据主表字段fdStatus进行判断。主表记录切换的时候将fdStatus传递到从表页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCM84_LayoutUpdated(object sender, EventArgs e)
        {
            String zbStatus = "上报";
            if (this.fdStatus.Value != null)
                zbStatus = (String)this.fdStatus.Value;
            if (zbStatus.Equals("已审批"))
            {
                Button updatebut = this.FindName("UPDATE") as Button;
                if (updatebut != null)
                    updatebut.IsEnabled = false;
            }
        }
        /// 修改前设置属性
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();

            _view.EFGridCheckedRows = this.sub_grid.GetCheckedEIRows();
            _view.ViewType = "tabitem";
            _view.Args.Add("fdApplicationId", this.fdApplicationId.Value.ToString());//外键赋值
            this.ViewProperty = _view;
        }



        /// <summary>
        /// 领料申请从表修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            List<string> list = new List<string>();
            list.Add("");

            int count = this.sub_grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择需要修改的明细数据！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("只能选择一条明细数据进行修改！", true);
                return;
            }
            else
            {
                IList<EIRow> _list = this.sub_grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.sub_grid.GetCheckedEIRows();
                (sender as Button).IsEnabled = false;

                EIInfo eiInfo = new EIInfo();
                EIBlock eIBlock = new EIBlock("result");

                EIRow row = new EIRow();
                if (_list.Count > 0)
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
                eIBlock.SetRow(0, row);
                eiInfo.AddBlock(eIBlock);

                base.OnUpdateButtonClickPopup(eiInfo, "领料明细-修改", "update", null, sender, e);
            }
        }

        /// <summary>
        /// 用于刷新主表页面
        /// </summary>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
            ExportApplicationDetail exportApplicationDetail = new ExportApplicationDetail(dialogType);//构造一个新的修改页面
            exportApplicationDetail.EIDataContext = new Platform.Core.UI.EIDataContext();
            exportApplicationDetail.EIDataContext.Update(eIInfo);
            exportApplicationDetail.fdApplicationNumber.Label = "申请数量";
            exportApplicationDetail.fdApplicationNumber.IsReadOnly = true;
            exportApplicationDetail.fdAuditNumber.Visibility = Visibility.Visible;

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = exportApplicationDetail;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                UpdateHomePageData(new EIInfo());
                this.sub_grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }        
    }
}

